package com.centling.smartSealForPhone.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.centling.smartSealForPhone.R;
import com.centling.smartSealForPhone.bean.AuditorSealRecordsBean;
import com.centling.smartSealForPhone.constants.UrlConstants;
import com.centling.smartSealForPhone.utils.ShowToast;
import com.example.localalbum.ui.UrlImageScrollView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Jessie
 * @date 2016/8/19
 */
public class AuditorReviewAdapter extends RecyclerView.Adapter<AuditorReviewAdapter.AuditorReviewViewHolder> {

    private Context context;
    private boolean isReviewLose;
    private List<AuditorSealRecordsBean> auditorSealRecordsBeanList;
    private ArrayList<String> files = new ArrayList<>();

    public AuditorReviewAdapter(Context context, List<AuditorSealRecordsBean> auditorSealRecordsBeanList) {
        this.context = context;
        this.auditorSealRecordsBeanList = auditorSealRecordsBeanList;
    }

    public int getCount() {
        int count = 0;
        for (AuditorSealRecordsBean auditorSealRecordsBean : auditorSealRecordsBeanList) {
            if (auditorSealRecordsBean.isChecked()) {
                count++;
            }
        }
        return count;
    }

    @SuppressLint("SdCardPath")
    private boolean isInstallByRead(String packageName) {
        return new File("/data/data/" + packageName).exists();
    }

    public void setReviewLose(boolean reviewLose) {
        isReviewLose = reviewLose;
    }

    @Override
    public AuditorReviewAdapter.AuditorReviewViewHolder onCreateViewHolder(ViewGroup parent,
                                                                           int viewType) {
        return new AuditorReviewViewHolder(
                LayoutInflater.from(context).inflate(R.layout.item_auditor_review, parent, false));
    }

    @Override
    public void onBindViewHolder(final AuditorReviewAdapter.AuditorReviewViewHolder holder, final int position) {
        if (isReviewLose) {
            holder.llLose.setVisibility(View.VISIBLE);
        } else {
            holder.llLose.setVisibility(View.GONE);
        }
        holder.checkBox.setChecked(auditorSealRecordsBeanList.get(position).isChecked());
        holder.checkBox.setClickable(false);
        holder.reviewName.setText(auditorSealRecordsBeanList.get(position).getOperator());

        //：0  授权状态 1  已盖章状态 2  图片上传完成状态  3 复核失败 4 图片补拍 5.补拍图片上传
        switch (auditorSealRecordsBeanList.get(position).getStatus()) {
            case "0":
                holder.reviewStatus.setText("授权");
                break;
            case "1":
                holder.reviewStatus.setText("已盖章");
                break;
            case "2":
                holder.reviewStatus.setText("图片上传完成");
                break;
            case "3":
                holder.reviewStatus.setText("复核失败");
                break;
            case "4":
                holder.reviewStatus.setText("图片补拍");
                break;
            case "5":
                holder.reviewStatus.setText("补拍图片上传");
                break;
            default:
                break;
        }

        if (auditorSealRecordsBeanList.get(position).getLatitude() == -200) {
            holder.map.setVisibility(View.GONE);
            holder.location.setText("章盾用印地址未上传");
        } else {
            holder.map.setVisibility(View.VISIBLE);
            holder.location.setText(auditorSealRecordsBeanList.get(position).getLocationName());
        }
        holder.map.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isInstallByRead("com.autonavi.minimap")) {
                    Intent gaoDeIntent = new Intent(Intent.ACTION_VIEW);
                    String url =
                            "androidamap://viewMap?sourceApplication=智能盖章&poiname=用印位置&lat="
                                    + auditorSealRecordsBeanList
                                    .get(position).getLatitude() + "&lon=" + auditorSealRecordsBeanList
                                    .get(position).getLongtitude() + "&dev=0";
                    Uri uri = Uri.parse(url);
                    gaoDeIntent.setData(uri);
                    context.startActivity(gaoDeIntent);
                } else {
                    ShowToast.shortTime("您的手机没有安装高德地图");
                }
            }
        });

        /*
          logic to display no pics placeholder
         */
        if (TextUtils.isEmpty(auditorSealRecordsBeanList.get(position).getSealRecordsPicPath())) {
            holder.noPic.setVisibility(View.VISIBLE);
            holder.reviewPic.setVisibility(View.GONE);
        } else {
            holder.noPic.setVisibility(View.GONE);
            holder.reviewPic.setVisibility(View.VISIBLE);
            files.clear();
            files.add(auditorSealRecordsBeanList.get(position).getSealRecordsPicPath());
            holder.reviewPic.getFiles().clear();
            holder.reviewPic.addFiles(files);
        }

        holder.llLose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.checkBox.setChecked(!auditorSealRecordsBeanList.get(position).isChecked());
                auditorSealRecordsBeanList.get(position).setChecked(
                        !auditorSealRecordsBeanList.get(position).isChecked());
            }
        });
    }

    @Override
    public int getItemCount() {
        return auditorSealRecordsBeanList.size();
    }

    class AuditorReviewViewHolder extends RecyclerView.ViewHolder {
        TextView reviewName, reviewStatus, reviewTime, reviewOperator, location, map;
        UrlImageScrollView reviewPic;
        CheckBox checkBox;
        LinearLayout llLose;
        ImageView noPic;

        AuditorReviewViewHolder(View itemView) {
            super(itemView);
            reviewPic = itemView.findViewById(R.id.iv_apply_review_add_file_pic);
            noPic = itemView.findViewById(R.id.iv_apply_review_no_pic);
            reviewName = itemView.findViewById(R.id.tv_auditor_review_operator_name);
            reviewStatus = itemView.findViewById(R.id.tv_auditor_review_status);
            location = itemView.findViewById(R.id.tv_auditor_review_location);
            map = itemView.findViewById(R.id.tv_auditor_review_map);
            reviewTime = itemView.findViewById(R.id.tv_auditor_review_time);
            reviewOperator = itemView.findViewById(R.id.tv_auditor_operator_review);
            checkBox = itemView.findViewById(R.id.ck_auditor_review_check);
            llLose = itemView.findViewById(R.id.ll_review_lose_check);
        }
    }
}
